/*VaWVqaRPOcVLGQLUZRykSxN43lbEnxQvSuDfiMhf4NYKgvGSAU5ZxOpOA4snWHov*/
/**
 * 固定资产卡片 列表态
 * @author zhangypm
 * @since ncc1.0 2018年5月11日
 */
import React, { Component } from 'react';
import { createPage, ajax, base, high, cardCache, cacheTools, createPageIcon } from 'nc-lightapp-front';
import constants from '../constants';
import ampub from 'ampub';
import fa from 'fa';

import CreateNCUploade from 'uap/common/components/CreateNCUploade';
const { createNCUploader, show: ncUploaderShow, close: ncUploaderClose } = CreateNCUploade;

import {
	searchBtnClick,
	clickAdvBtnEve,
	pageInfoClick,
	initTemplate,
	buttonClick,
	doubleClick,
	afterEvent,
    orgAfterEvent,
	rowSelected,
	linkedQueryUtil
} from './events';

import FaCardCopy from '../components/CardCopy';
import '../components/CardCopy.less';

const {
	components: { queryVocherSrcUtils: { queryVoucherSrc } },
	utils: {
		listUtils: { createListTitleArea, creatListSocket },
		msgUtils: { showMessage },
		multiLangUtils: { getMultiLangByID }
	}
} = ampub;

// 参照过滤业务单元
const { components: { assetOrgMultiRefFilter: { renderCompleteEvent } } } = ampub;

const { assetDataSource, AREA: { LIST: { SEARCH_AREA: searchId, GRID_AREA: tableId } } } = constants;

const { NCDiv } = base;

class List extends Component {
	constructor(props) {
		super(props);

		props.use.form('copyCard');
		props.use.search(searchId);
		props.use.table(tableId);

		this.facardCopy = new FaCardCopy('list');

		this.state = {
			uploader: {} //附件上传
		};

		this.isFirstLoad = true; //高级按钮第一次点击

		this.templateid = ''; //模板主键

		this.doInit.bind(this);

		this.doInit().then(() => {
			initTemplate.call(this, this.props);
		});
	}

	componentDidMount() {
		console.log('---------------------  componentDidMount linkedQueryUtil=', linkedQueryUtil);
		console.log('---------------------  componentDidMount linkedQueryByVoucher=', linkedQueryUtil.linkedQueryByVoucher);

		//凭证联查来源单据
		queryVoucherSrc(
			this.props,
			tableId,
			'pk_card',
			constants.PAGE_CODE.LIST,
			linkedQueryUtil.linkedQueryByVoucher.bind(this)
		);

		//其他单据联查卡片到列表态（如：工程转固单按数量拆分建卡生成多张卡片时，联查时需要到列表态）
		linkedQueryUtil.linkedQueryByBill.call(this, this.props);
	}

	//初始化
	doInit() {
		const that = this;
		const promise = new Promise(function(resolve, reject) {
			ajax({
				url: constants.URL.INIT,
				data: {},
				success: (res) => {
					let { success, data } = res;
					if (success) {
						that.state.transiTypeAry = data.billTypeAry;

						if (data.msgs && data.msgs.length) {
							showMessage(null, { content: data.msgs.join(','), color: 'warning' });
						}

						resolve(data);
					} else {
						reject(data);
					}
				},
				error: (err) => {
					showMessage(null, { content: err.message, color: 'danger' });
					reject(err);
				}
			});
		});
		return promise;
	}

	//删除对话框 确认按钮
	delConfirm = () => {
		buttonClick.call(this, this.props, 'deleteConfirm');
	};

	render() {
		let { table, search, button, modal, ncmodal, pageConfig } = this.props;
		let { createSimpleTable } = table;
		let { NCCreateSearch } = search;
		let { createModal } = ncmodal;
		let { createButtonApp } = button;

		return (
			<div className="nc-bill-list">
				<NCDiv areaCode={NCDiv.config.HEADER} className="nc-bill-header-area">
					{/* 标题区 */}
					<div className="header-title-search-area">
						{createListTitleArea.call(this, this.props, { title: pageConfig.title })}
					</div>

					<div className="header-button-area">
						{createButtonApp({
							area: 'list_head',
							buttonLimit: 10,
							onButtonClick: buttonClick.bind(this),
							popContainer: document.querySelector('.btn-group')
						})}
					</div>
				</NCDiv>
				<div className="nc-bill-search-area">
					{NCCreateSearch(searchId, {
						clickAdvBtnEve: clickAdvBtnEve.bind(this), //高级按钮点击前事件
						clickSearchBtn: searchBtnClick.bind(this),
						onAfterEvent: afterEvent.bind(this),
                        // 查询区渲染后：主组织关联参照字段控制其业务单元是否显示
						renderCompleteEvent: renderCompleteEvent.bind(
							this,
							this.props,
							searchId,
							'pk_org',
							orgAfterEvent.bind(this)
						),
                        // 优先级低于clickAdvBtnEve
                        statusChangeEvent: renderCompleteEvent.bind(
							this,
							this.props,
							searchId,
							'pk_org',
							orgAfterEvent.bind(this)
						)
					})}
				</div>
				<div className="nc-bill-table-area">
					{createSimpleTable(tableId, {
						dataSource: assetDataSource,
						pkname: 'pk_cardhistory',
						handlePageInfoChange: pageInfoClick,
						onSelected: rowSelected.bind(this),
						onSelectedAll: rowSelected.bind(this),
						showIndex: true,
						showCheck: true,
						onRowDoubleClick: doubleClick.bind(this),
						componentInitFinished: () => {
							//缓存数据赋值成功的钩子函数
							//若初始化数据后需要对数据做修改，可以在这里处理
							rowSelected.call(this, this.props, tableId);
						}
					})}
				</div>

				{modal.createModal(constants.MODAL_ID.CopyModal, {
					title: getMultiLangByID('201201504A-000026') /* 国际化处理： 卡片复制 */,
					content: this.facardCopy.copyCardDialog(this),
					userControl: true,
					className: 'combine'
				})}
				{/* 附件*/}
				{createNCUploader.call(this, 'uploader', {})}
				{/* 创建 socket 链接 */}
				{creatListSocket(this.props, {
					tableAreaCode: tableId, //表格区域编码
					billpkname: constants.FIELDS.HEAD_PK //表格主键
					//billtype: pageConfig.billType,//单据类型,用于查询追溯
				})}
			</div>
		);
	}
}

export default List;

/*VaWVqaRPOcVLGQLUZRykSxN43lbEnxQvSuDfiMhf4NYKgvGSAU5ZxOpOA4snWHov*/